configuration.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import logging
  2. import os
  3. import subprocess
  4. from pip._internal.cli.base_command import Command
  5. from pip._internal.cli.status_codes import ERROR, SUCCESS
  6. from pip._internal.configuration import (
  7. Configuration,
  8. get_configuration_files,
  9. kinds,
  10. )
  11. from pip._internal.exceptions import PipError
  12. from pip._internal.utils.logging import indent_log
  13. from pip._internal.utils.misc import get_prog, write_output
  14. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  15. if MYPY_CHECK_RUNNING:
  16. from typing import List, Any, Optional
  17. from optparse import Values
  18. from pip._internal.configuration import Kind
  19. logger = logging.getLogger(__name__)
  20. class ConfigurationCommand(Command):
  21. """
  22. Manage local and global configuration.
  23. Subcommands:
  24. - list: List the active configuration (or from the file specified)
  25. - edit: Edit the configuration file in an editor
  26. - get: Get the value associated with name
  27. - set: Set the name=value
  28. - unset: Unset the value associated with name
  29. - debug: List the configuration files and values defined under them
  30. If none of --user, --global and --site are passed, a virtual
  31. environment configuration file is used if one is active and the file
  32. exists. Otherwise, all modifications happen on the to the user file by
  33. default.
  34. """
  35. ignore_require_venv = True
  36. usage = """
  37. %prog [<file-option>] list
  38. %prog [<file-option>] [--editor <editor-path>] edit
  39. %prog [<file-option>] get name
  40. %prog [<file-option>] set name value
  41. %prog [<file-option>] unset name
  42. %prog [<file-option>] debug
  43. """
  44. def add_options(self):
  45. # type: () -> None
  46. self.cmd_opts.add_option(
  47. '--editor',
  48. dest='editor',
  49. action='store',
  50. default=None,
  51. help=(
  52. 'Editor to use to edit the file. Uses VISUAL or EDITOR '
  53. 'environment variables if not provided.'
  54. )
  55. )
  56. self.cmd_opts.add_option(
  57. '--global',
  58. dest='global_file',
  59. action='store_true',
  60. default=False,
  61. help='Use the system-wide configuration file only'
  62. )
  63. self.cmd_opts.add_option(
  64. '--user',
  65. dest='user_file',
  66. action='store_true',
  67. default=False,
  68. help='Use the user configuration file only'
  69. )
  70. self.cmd_opts.add_option(
  71. '--site',
  72. dest='site_file',
  73. action='store_true',
  74. default=False,
  75. help='Use the current environment configuration file only'
  76. )
  77. self.parser.insert_option_group(0, self.cmd_opts)
  78. def run(self, options, args):
  79. # type: (Values, List[str]) -> int
  80. handlers = {
  81. "list": self.list_values,
  82. "edit": self.open_in_editor,
  83. "get": self.get_name,
  84. "set": self.set_name_value,
  85. "unset": self.unset_name,
  86. "debug": self.list_config_values,
  87. }
  88. # Determine action
  89. if not args or args[0] not in handlers:
  90. logger.error(
  91. "Need an action (%s) to perform.",
  92. ", ".join(sorted(handlers)),
  93. )
  94. return ERROR
  95. action = args[0]
  96. # Determine which configuration files are to be loaded
  97. # Depends on whether the command is modifying.
  98. try:
  99. load_only = self._determine_file(
  100. options, need_value=(action in ["get", "set", "unset", "edit"])
  101. )
  102. except PipError as e:
  103. logger.error(e.args[0])
  104. return ERROR
  105. # Load a new configuration
  106. self.configuration = Configuration(
  107. isolated=options.isolated_mode, load_only=load_only
  108. )
  109. self.configuration.load()
  110. # Error handling happens here, not in the action-handlers.
  111. try:
  112. handlers[action](options, args[1:])
  113. except PipError as e:
  114. logger.error(e.args[0])
  115. return ERROR
  116. return SUCCESS
  117. def _determine_file(self, options, need_value):
  118. # type: (Values, bool) -> Optional[Kind]
  119. file_options = [key for key, value in (
  120. (kinds.USER, options.user_file),
  121. (kinds.GLOBAL, options.global_file),
  122. (kinds.SITE, options.site_file),
  123. ) if value]
  124. if not file_options:
  125. if not need_value:
  126. return None
  127. # Default to user, unless there's a site file.
  128. elif any(
  129. os.path.exists(site_config_file)
  130. for site_config_file in get_configuration_files()[kinds.SITE]
  131. ):
  132. return kinds.SITE
  133. else:
  134. return kinds.USER
  135. elif len(file_options) == 1:
  136. return file_options[0]
  137. raise PipError(
  138. "Need exactly one file to operate upon "
  139. "(--user, --site, --global) to perform."
  140. )
  141. def list_values(self, options, args):
  142. # type: (Values, List[str]) -> None
  143. self._get_n_args(args, "list", n=0)
  144. for key, value in sorted(self.configuration.items()):
  145. write_output("%s=%r", key, value)
  146. def get_name(self, options, args):
  147. # type: (Values, List[str]) -> None
  148. key = self._get_n_args(args, "get [name]", n=1)
  149. value = self.configuration.get_value(key)
  150. write_output("%s", value)
  151. def set_name_value(self, options, args):
  152. # type: (Values, List[str]) -> None
  153. key, value = self._get_n_args(args, "set [name] [value]", n=2)
  154. self.configuration.set_value(key, value)
  155. self._save_configuration()
  156. def unset_name(self, options, args):
  157. # type: (Values, List[str]) -> None
  158. key = self._get_n_args(args, "unset [name]", n=1)
  159. self.configuration.unset_value(key)
  160. self._save_configuration()
  161. def list_config_values(self, options, args):
  162. # type: (Values, List[str]) -> None
  163. """List config key-value pairs across different config files"""
  164. self._get_n_args(args, "debug", n=0)
  165. self.print_env_var_values()
  166. # Iterate over config files and print if they exist, and the
  167. # key-value pairs present in them if they do
  168. for variant, files in sorted(self.configuration.iter_config_files()):
  169. write_output("%s:", variant)
  170. for fname in files:
  171. with indent_log():
  172. file_exists = os.path.exists(fname)
  173. write_output("%s, exists: %r",
  174. fname, file_exists)
  175. if file_exists:
  176. self.print_config_file_values(variant)
  177. def print_config_file_values(self, variant):
  178. # type: (Kind) -> None
  179. """Get key-value pairs from the file of a variant"""
  180. for name, value in self.configuration.\
  181. get_values_in_config(variant).items():
  182. with indent_log():
  183. write_output("%s: %s", name, value)
  184. def print_env_var_values(self):
  185. # type: () -> None
  186. """Get key-values pairs present as environment variables"""
  187. write_output("%s:", 'env_var')
  188. with indent_log():
  189. for key, value in sorted(self.configuration.get_environ_vars()):
  190. env_var = 'PIP_{}'.format(key.upper())
  191. write_output("%s=%r", env_var, value)
  192. def open_in_editor(self, options, args):
  193. # type: (Values, List[str]) -> None
  194. editor = self._determine_editor(options)
  195. fname = self.configuration.get_file_to_edit()
  196. if fname is None:
  197. raise PipError("Could not determine appropriate file.")
  198. try:
  199. subprocess.check_call([editor, fname])
  200. except subprocess.CalledProcessError as e:
  201. raise PipError(
  202. "Editor Subprocess exited with exit code {}"
  203. .format(e.returncode)
  204. )
  205. def _get_n_args(self, args, example, n):
  206. # type: (List[str], str, int) -> Any
  207. """Helper to make sure the command got the right number of arguments
  208. """
  209. if len(args) != n:
  210. msg = (
  211. 'Got unexpected number of arguments, expected {}. '
  212. '(example: "{} config {}")'
  213. ).format(n, get_prog(), example)
  214. raise PipError(msg)
  215. if n == 1:
  216. return args[0]
  217. else:
  218. return args
  219. def _save_configuration(self):
  220. # type: () -> None
  221. # We successfully ran a modifying command. Need to save the
  222. # configuration.
  223. try:
  224. self.configuration.save()
  225. except Exception:
  226. logger.exception(
  227. "Unable to save configuration. Please report this as a bug."
  228. )
  229. raise PipError("Internal Error.")
  230. def _determine_editor(self, options):
  231. # type: (Values) -> str
  232. if options.editor is not None:
  233. return options.editor
  234. elif "VISUAL" in os.environ:
  235. return os.environ["VISUAL"]
  236. elif "EDITOR" in os.environ:
  237. return os.environ["EDITOR"]
  238. else:
  239. raise PipError("Could not determine editor to use.")